package com.qf.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.qf.entity.Orders;
import com.qf.entity.WebOrder;
import com.qf.exception.BizException;

import com.qf.mapper.OrdersMapper;
import com.qf.service.IOrdersService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
@Transactional
public class OrdersServiceImpl implements IOrdersService {
    private final String imageServerLocation = "http://localhost/images/";
    private OrdersMapper ordersMapper;

    public OrdersServiceImpl(OrdersMapper ordersMapper){
        this.ordersMapper=ordersMapper;
    }
    @Override
    public List<Orders> queryById( int oStatus , int oUserId)throws BizException {
        List<Orders> list = null;
        Orders orders = null;
        try {
            list  =  ordersMapper.queryById( oStatus , oUserId);
        }catch (Exception e){
            e.printStackTrace();
            throw new BizException("业务层查询异常: " + e.getMessage());
        }
        return list;
    }

    @Override
    public List<Orders> QueryByStatus(Integer status, Integer id) {
        return ordersMapper.selectNoPay(0,28);
    }

    @Override
    public List<WebOrder> getOrdersByStatus(Integer status, Integer userId) {


        List<Orders> ordersList = ordersMapper.selectNoPay(status,userId);


        List<WebOrder> webOrderList = new ArrayList<>();

        Map<String,List<Orders>> map = new HashMap<>();
        Map<String, Date> orderTimeMap = new HashMap<>();
        Map<String, BigDecimal> orderMoneyMap = new HashMap<>();

        for (Orders o : ordersList){
            String orderNo = o.getoNo(); //订单的Id
            Date createTime = o.getoCreateTime(); //订单的创建日期
            BigDecimal oneMoney = o.getoGoodPrice(); //订单单个价格
            BigDecimal oneNum = new BigDecimal(o.getoNum()); //订单单个商品的数量
            o.setoGoodImg(imageServerLocation+o.getoGoodImg()); //商品图片

            //获取改订单编号的创建日期
            Date createDate = orderTimeMap.get(orderNo);
            if (null == createDate){
                orderTimeMap.put(orderNo, createTime);
            }

            //计算改订单的总金额
            BigDecimal money = orderMoneyMap.get(orderNo);
            if (null == money){
                orderMoneyMap.put(orderNo,oneMoney.multiply(oneNum));
            }else {
                orderMoneyMap.put(orderNo,(oneMoney.multiply(oneNum)).add(money));
            }

            //获取该订单编号对应的所有的订单
            List<Orders> orders = map.get(orderNo);
            if (null == orders){ //该订单编号没有对应的订单
                orders = new ArrayList<>();
                orders.add(o);
                map.put(orderNo , orders);
            }else { //该订单编号有订单了
                orders.add(o);
            }
        }

        map.forEach((k, v) -> {
            WebOrder webOrder = new WebOrder();

            webOrder.setOrderNo(k);
            webOrder.setOrders(v);

            Date createTime = orderTimeMap.get(k);  //订单的创建日期

            webOrder.setCreateTime(orderTimeMap.get(k));

            webOrder.setOrderMoney(orderMoneyMap.get(k));

            webOrderList.add(webOrder);
        });

        return webOrderList;
    }

    @Override
    public List<Orders> ChangeStatus(String no, Integer status) {
        return ordersMapper.ChangeStatus(no,status);
    }

    @Override
    public void timeOutOrder() {
        ordersMapper.timeoutOrder();
    }

    @Override
    public Integer batchInsert(List<Orders> ordersList) {
        Integer integer = ordersMapper.batchInsert(ordersList);
        return integer;
    }

    @Override
    public List<Orders> selectNoPay(int status, int userId) {
        List<Orders> orders = ordersMapper.selectNoPay(status, userId);
        return orders;
    }

    @Override
    public List<Orders> queryAll(Orders orders) {
        List<Orders> ordersList = ordersMapper.queryAll(orders);
        return ordersList;
    }

    @Override
    public int deleteById(String oNo) {
        int deleteById = ordersMapper.deleteById(oNo);
        return deleteById;
    }


}
